let express = require("express");
let bodyParser = require("body-parser");
let fs = require("fs");
let web = express();
web.use(express.static("public"));
web.use(bodyParser.urlencoded({extended:true}))
web.get("/list",(req,res)=>{
    fs.readFile("./data/stu.json","utf-8",(err,data)=>{
        if(err){
            res.send("获取失败")
        }else{
            data = JSON.parse(data);
            res.send(data)
        }
    })  
})

// 搜索学生接口
web.get("/search/stu",(req,res)=>{
    // 获取get请求传递的参数
    console.log(req.query)
    let {username,age,sex} = req.query;
    console.log(age,sex)
    fs.readFile("./data/stu.json","utf-8",(err,data)=>{
        if(err){
            res.send("查询失败")
        }else{
            data = JSON.parse(data);
            let newArry = data.filter(item=>{
                return item.name === username
            })
            res.send(newArry)
        }
    })  
})
web.post("/get/move",(req,res)=>{
    res.send("卡啦。")
})
web.post("/get/msg",(req,res)=>{
    console.log(req.body)
    let {name,age} = req.body;
    res.send(`我叫${name}今年${age}`)
})
web.listen(8080,()=>{
    console.log("服务端已启动....")
})